
var oApplyDetailDialog = {
	dialog: null,
	oThis: null,

	detail: null,

	doctorInfo: [],

	hzInfo: null, // 患者信息
	yxInfo: null, // 影像信息

	init: function () {
		this.initState();
		this.initEvent();
	},

	initState: function () {
		this.dialog = new TheDialog('#detailDialog');
		this.oThis = $('#detailDialog');

		
	},

	initEvent: function () {
		var _this = this;

		// 选择文件点击
		$('#choiseFileBtn').on('click', function () {
			applyFileDialog.show();
		})

		// 确认按钮
		this.oThis.find('.submit-btn').on('click', function () {
			_this.submit();
		})
	},

	show: function (detail) {
		this.detail = detail;

		this.oThis.find('._header .title').html(this.detail ? '修改影像申请' : '添加影像申请');

		this.dialog.show();

		this.getDoctorInfo(); // 获取科室医生信息

		// 根据详情设置内容
		this.setDetailHtml();
	},

	// 获取科室医生信息
	getDoctorInfo: function () {
		var _this = this;
		$.ajax({
			type: 'get',
			url: pb.api + '/baImageApply/user',
			success: function (res) {
				var info = res.data;
				_this.oThis.find('.sqks-ipt').val(info.deptName);
				_this.oThis.find('.sqys-ipt').val(info.userName);
				_this.doctorInfo = info;
			}
		})
	},

	// 保存
	submit: function () {
		var _this = this;
		var msg = '';

		if (!this.yxInfo) {
			Qmsg.error('请选择影像信息');
			return;
		}
		var bwId = this.oThis.find('input[name=bwradio]:checked').val();
		var bwText = pb.getNameByCode(bwId, pb.dzbwList);
		if (!bwId) {
			Qmsg.error('请选择定制部位');
			return;
		}
		var diagnosisMark = this.oThis.find('.dzzd-ipt').val();
		if (!bwId) {
			Qmsg.error('请输入疾病诊断');
			return;
		}
		var lyId = this.oThis.find('input[name=lyradio]:checked').val();
		var lyText = lyId == 3 ? this.oThis.find('.ly-else-ipt').val() : pb.getNameByCode(lyId, pb.dzlyList);
		if (!lyId) {
			Qmsg.error('请选择定制理由');
			return;
		}
		var lxId = this.oThis.find('input[name=lxradio]:checked').val();
		var lxText = pb.getNameByCode(lxId, pb.yxsjlxList);
		if (!lxId) {
			Qmsg.error('请选择影像数据类型');
			return;
		}

		var param = {
			deptId: this.doctorInfo.deptCode,
			deptName: this.doctorInfo.deptName, // 申请科室
			doctorId: this.doctorInfo.userCode,
			doctorName: this.doctorInfo.userName, // 申请医生

			// 患者
			patientName: this.hzInfo.patientName, // 患者姓名
			sex: this.hzInfo.sex, // 患者性别
			age: this.hzInfo.age, // 患者年龄
			outpatientCode: this.hzInfo.outpatientCode, // 门诊号
			hospitalizedCode: this.hzInfo.hospitalizedCode, // 住院号
			inspectionCode: this.hzInfo.inspectionCode, // 检查号
			imageCode: this.hzInfo.imageCode, // 影像号
			patientTypeName: this.hzInfo.patientType, // 患者类型
			checkPointMethod: this.hzInfo.checkPointMethod, // 检查部位
			equipmentDetail: this.hzInfo.equipmentDetail, // 设备明细
			// 影像
			imageId: this.yxInfo.id, // 影像编码
			imageName: this.yxInfo.imageName, // 影像名称
			imageNuber: this.yxInfo.imageNuber, // 图像数
			imagePath: this.yxInfo.imagePath, // 影像地址
			seriaiuid: this.yxInfo.seriaiuid, // SerialUID
			sequenceMark: this.yxInfo.sequenceMark, // 序列描述

			partId: bwId, // 定制部位id
			partName: bwText, // 定制部位名称
			diagnosisMark: diagnosisMark, // 疾病诊断
			reasonCode: lyId, // 定制理由编码
			reasonMark: lyText, // 定制理由
			imageTypeCode: lxId, // 影像数据类型编码
			// partId: lxText, // 定制部位id
		}
		if (this.detail) {
			param.id = this.detail.id;
		}
		$.ajax({
			type: 'post',
			url: pb.api + '/baImageApply/save',
			data: param,
			success: function (res) {
				if (res.result == "true") {
					_this.dialog.close();
					Qmsg.success((_this.detail ? '修改' : '添加') + '成功');
					oPage.getData();
				} else {
					Qmsg.error(res.message);
				}
			}
		})
	},

	// 设置详情信息
	setDetailHtml: function () {
		var _this = this;
		this.oThis.find('.sqks-ipt').val(this.detail ? this.detail.deptName : '');
		this.oThis.find('.sqys-ipt').val(this.detail ? this.detail.doctorName : '');
	
		// 设置影像患者信息
		var hz = this.detail ? {
			patientName: this.detail.patientName,
			sex: this.detail.sex,
			age: this.detail.age,
			outpatientCode: this.detail.outpatientCode,
			hospitalizedCode: this.detail.hospitalizedCode,
			inspectionCode: this.detail.inspectionCode,
			imageCode: this.detail.imageCode,
			patientType: this.detail.patientTypeName,
			checkPointMethod: this.detail.checkPointMethod,
			equipmentDetail: this.detail.equipmentDetail,
		} : null;
		var yx = this.detail ? {
			id: this.detail.imageId,
			imageName: this.detail.imageName,
			imageNuber: this.detail.imageNuber,
			imagePath: this.detail.imagePath,
			seriaiuid: this.detail.seriaiuid,
			sequenceMark: this.detail.sequenceMark,
		} : null;
		this.setHzYx(hz, yx);

		// 定制部位
		this.oThis.find('input[name=bwradio]').map(function () {
			var oItem = $(this);
			if (_this.detail && oItem.val() == _this.detail.partId) {
				oItem.attr('checked', 'checked');
			} else {
				oItem.removeAttr('checked');
			}
		})
		// 疾病诊断
		this.oThis.find('.dzzd-ipt').val(this.detail ? this.detail.diagnosisMark : '');
		// 定制理由
		this.oThis.find('input[name=lyradio]').map(function () {
			var oItem = $(this);
			if (_this.detail && oItem.val() == _this.detail.reasonCode) {
				oItem.attr('checked', 'checked');
			} else {
				oItem.removeAttr('checked');
			}
		})
		this.oThis.find('.ly-else-ipt').val(this.detail && this.detail.reasonCode == 3 ? this.detail.reasonMark : '');
		// 影像数据类型
		this.oThis.find('input[name=lxradio]').map(function () {
			var oItem = $(this);
			if (_this.detail && oItem.val() == _this.detail.imageTypeCode) {
				oItem.attr('checked', 'checked');
			} else {
				oItem.removeAttr('checked');
			}
		})
	},

	// 设置影像患者信息
	setHzYx: function (hz, yx)  {
		this.hzInfo = hz;
		this.yxInfo = yx;

		// 患者
		this.oThis.find('.hz-name').html(hz ? hz.patientName : '-');
		this.oThis.find('.hz-sex').html(hz ? hz.sex : '-');
		this.oThis.find('.hz-age').html(hz ? hz.age : '-');
		this.oThis.find('.hz-mz').html(hz ? hz.outpatientCode : '-');
		this.oThis.find('.hz-zy').html(hz ? hz.hospitalizedCode : '-');
		this.oThis.find('.hz-jc').html(hz ? hz.inspectionCode : '-');
		this.oThis.find('.hz-yx').html(hz ? hz.imageCode : '-');
		// 影像
		this.oThis.find('.yxxx-ipt').val(yx ? yx.imageName : '');
	},

}


$(function () {
	oApplyDetailDialog.init();
})